﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BaseDll;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.ComponentModel;

namespace OtherDevice
{
    [Description("Plasma控制器")]
    public class PlasmaSet : PlasmaPowerBase
    {

        /// <summary>
        /// 初始化Plasma串口
        /// </summary>
        /// <param name="CommPort"></param>
        /// <returns></returns>
        public override int Init(string CommPort,string parameter = "38400,n,8,1")
        {
            int ret = InitComAddEvent(CommPort, parameter);
            if (ret != (int)0)
            {
                return -1;
            }
            bStatus = true;
            return 0;
        }

        /// <summary>
        /// 打开Plasma（喷火）01 05 00 00 FF 00 8C 3A 
        /// </summary>
        /// <param name="b"></param>
        /// <returns></returns>
        public override bool Open()
        {
            byte[] data = new byte[6];
            data[0] = 0x01;
            data[1] = 0x05;
            data[2] = 0x00;
            data[3] = 0x01;
            data[4] = 0xFF;
            data[5] = 0x00;
            if (Received(data).Count() == 8)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 关闭Plasma（停止喷火）01 05 00 00 00 00 CD CA 
        /// </summary>
        /// <param name="b"></param>
        /// <returns></returns>
        public override bool Close()
        {
            byte[] data = new byte[6];
            data[0] = 0x01;
            data[1] = 0x05;
            data[2] = 0x00;
            data[3] = 0x01;
            data[4] = 0x00;
            data[5] = 0x00;
            if (Received(data).Count() == 8)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 设置plasma额定功率
        /// </summary>
        /// <returns></returns>
        public override bool SetMaxPower()
        {
            int a = ParamSetMgr.GetInstance().GetIntParam("Plasma功率设置值");
            byte[] data = new byte[6];
            data[0] = 0x01;
            data[1] = 0x06;
            data[2] = 0x00;
            data[3] = 0x01;
            data[4] = (byte)((a & 0xff00) >> 8);
            data[5] = (byte)((short)a & 0x00FF); ;
            if (Received(data).Count() == 8)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 设定plasma最小功率
        /// </summary>
        /// <returns></returns>
        public override bool SetMinPower()
        {
            if (!ParamSetMgr.GetInstance().GetBoolParam("串口控制方式"))
            {
                return true;
            }

            int a = ParamSetMgr.GetInstance().GetIntParam("Plasma功率报警值");
            byte[] data = new byte[6];
            data[0] = 0x01;
            data[1] = 0x06;
            data[2] = 0x00;
            data[3] = 0x02;
            data[4] = (byte)((a & 0xff00) >> 8);
            data[5] = (byte)((short)a & 0x00FF); ;
            if (Received(data).Count() == 8)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 设置最小气压 ParamSetMgr.GetInstance().GetIntParam("Plasma气压报警值")
        /// </summary>
        /// <param name="a">ParamSetMgr.</param>
        /// <returns></returns>
        public override bool SetMinAir(int a)
        {
            if (!ParamSetMgr.GetInstance().GetBoolParam("串口控制方式"))
            {
                return true;
            }
            if (a >= 5)
                a = a - 5;
            byte[] data = new byte[6];
            data[0] = 0x01;
            data[1] = 0x06;
            data[2] = 0x00;
            data[3] = 0x06;
            data[4] = (byte)((a & 0xff00) >> 8);
            data[5] = (byte)((short)a & 0x00FF); ;
            if (Received(data).Count() == 8)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 获取报警信息 实际使用时 e=string er = ""
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public override bool GetAlarm(ref string e)
        {
            byte[] data = new byte[6];
            data[0] = 0x01;
            data[1] = 0x02;
            data[2] = 0x00;
            data[3] = 0x01;
            data[4] = 0x00;
            data[5] = 0x01;
            #region 电压过高
            byte[] result = Received(data);
            if (result.Count() == 8)
            {
                if (((result[3] != 0x00) && (result[7] == 0x00) && (result[6] == 0x00)))
                {
                    e = "电压过高";
                    return false;
                }
                if ((result[0] == 0x00) && (result[1] == 0x00) && (result[2] == 0x00))
                {
                    e = "串口失连,可能plasma断电";
                    return false;
                }
            }
            else
            {
                e = $"读取位置{ data[3] }返回值个数为{result.Count()}";
                return false;
            }
            #endregion
            #region 电压过低
            data[3] = 0x02;
            result = Received(data);
            if (result.Count() == 8)
            {
                if (((result[3] != 0x00) && (result[7] == 0x00) && (result[6] == 0x00)))
                {
                    e = "电压过低";
                    return false;
                }
                if ((result[0] == 0x00) && (result[1] == 0x00) && (result[2] == 0x00))
                {
                    e = "串口失连,可能plasma断电";
                    return false;
                }
            }
            else
            {
                e = $"读取位置{ data[3] }返回值个数为{result.Count()}";
                return false;
            }
            #endregion
            #region 电压不稳定
            data[3] = 0x03;
            result = Received(data);
            if (result.Count() == 8)
            {
                if (((result[3] != 0x00) && (result[7] == 0x00) && (result[6] == 0x00)))
                {
                    e = "电压不稳定";
                    return false;
                }
                if ((result[0] == 0x00) && (result[1] == 0x00) && (result[2] == 0x00))
                {
                    e = "串口失连,可能plasma断电";
                    return false;
                }
            }
            else
            {
                e = $"读取位置{ data[3] }返回值个数为{result.Count()}";
                return false;
            }
            #endregion
            #region 气压过高
            data[3] = 0x04;
            result = Received(data);
            if (result.Count() == 8)
            {
                if (((result[3] != 0x00) && (result[7] == 0x00) && (result[6] == 0x00)))
                {
                    e = "气压过高";
                    return false;
                }
                if ((result[0] == 0x00) && (result[1] == 0x00) && (result[2] == 0x00))
                {
                    e = "串口失连,可能plasma断电";
                    return false;
                }
            }
            else
            {
                e = $"读取位置{ data[3] }返回值个数为{result.Count()}";
                return false;
            }
            #endregion
            #region 气压过低
            data[3] = 0x05;
            result = Received(data);
            if (result.Count() == 8)
            {
                if (((result[3] != 0x00) && (result[7] == 0x00) && (result[6] == 0x00)))
                {
                    e = "气压过低";
                    return false;
                }
                if ((result[0] == 0x00) && (result[1] == 0x00) && (result[2] == 0x00))
                {
                    e = "串口失连,可能plasma断电";
                    return false;
                }
            }
            else
            {
                e = $"读取位置{ data[3] }返回值个数为{result.Count()}";
                return false;
            }
            #endregion
            #region 气压不稳定
            data[3] = 0x06;
            result = Received(data);
            if (result.Count() == 8)
            {
                if (((result[3] != 0x00) && (result[7] == 0x00) && (result[6] == 0x00)))
                {
                    e = "气压不稳定";
                    return false;
                }
                if ((result[0] == 0x00) && (result[1] == 0x00) && (result[2] == 0x00))
                {
                    e = "串口失连,可能plasma断电";
                    return false;
                }
            }
            else
            {
                e = $"读取位置{ data[3] }返回值个数为{result.Count()}";
                return false;
            }
            #endregion
            #region 高压输出开路
            data[3] = 0x07;
            result = Received(data);
            if (result.Count() == 8)
            {
                if (((result[3] != 0x00) && (result[7] == 0x00) && (result[6] == 0x00)))
                {
                    e = "高压输出开路";
                    return false;
                }
                if ((result[0] == 0x00) && (result[1] == 0x00) && (result[2] == 0x00))
                {
                    e = "串口失连,可能plasma断电";
                    return false;
                }
            }
            else
            {
                e = $"读取位置{ data[3] }返回值个数为{result.Count()}";
                return false;
            }
            #endregion
            #region 高压输出短路
            data[3] = 0x08;
            result = Received(data);
            if (result.Count() == 8)
            {
                if (((result[3] != 0x00) && (result[7] == 0x00) && (result[6] == 0x00)))
                {
                    e = "高压输出短路";
                    return false;
                }
                if ((result[0] == 0x00) && (result[1] == 0x00) && (result[2] == 0x00))
                {
                    e = "串口失连,可能plasma断电";
                    return false;
                }
            }
            else
            {
                e = $"读取位置{ data[3] }返回值个数为{result.Count()}";
                return false;
            }
            #endregion
            #region 电机电流过高
            data[3] = 0x09;
            result = Received(data);
            if (result.Count() == 8)
            {
                if (((result[3] != 0x00) && (result[7] == 0x00) && (result[6] == 0x00)))
                {
                    e = "电机电流过高";
                    return false;
                }
                if ((result[0] == 0x00) && (result[1] == 0x00) && (result[2] == 0x00))
                {
                    e = "串口失连,可能plasma断电";
                    return false;
                }
            }
            else
            {
                e = $"读取位置{ data[3] }返回值个数为{result.Count()}";
                return false;
            }
            #endregion
            #region 电机电流过低
            data[3] = 0x0A;
            result = Received(data);
            if (result.Count() == 8)
            {
                if (((result[3] != 0x00) && (result[7] == 0x00) && (result[6] == 0x00)))
                {
                    e = "电机电流过低";
                    return false;
                }
                if ((result[0] == 0x00) && (result[1] == 0x00) && (result[2] == 0x00))
                {
                    e = "串口失连,可能plasma断电";
                    return false;
                }
            }
            else
            {
                e = $"读取位置{ data[3] }返回值个数为{result.Count()}";
                return false;
            }
            #endregion
            #region 电机电流不稳定
            data[3] = 0x0B;
            result = Received(data);
            if (result.Count() == 8)
            {
                if (((result[3] != 0x00) && (result[7] == 0x00) && (result[6] == 0x00)))
                {
                    e = "电机电流不稳定";
                    return false;
                }
                if ((result[0] == 0x00) && (result[1] == 0x00) && (result[2] == 0x00))
                {
                    e = "串口失连,可能plasma断电";
                    return false;
                }
            }
            else
            {
                e = $"读取位置{ data[3] }返回值个数为{result.Count()}";
                return false;
            }
            #endregion
            #region 转速过低
            data[3] = 0x0C;
            result = Received(data);
            if (result.Count() == 8)
            {
                if (((result[3] != 0x00) && (result[7] == 0x00) && (result[6] == 0x00)))
                {
                    e = "转速过低";
                    return false;
                }
                if ((result[0] == 0x00) && (result[1] == 0x00) && (result[2] == 0x00))
                {
                    e = "串口失连,可能plasma断电";
                    return false;
                }
            }
            else
            {
                e = $"读取位置{ data[3] }返回值个数为{result.Count()}";
                return false;
            }
            #endregion
            #region 转速不稳定
            data[3] = 0x0D;
            result = Received(data);
            if (result.Count() == 8)
            {
                if (((result[3] != 0x00) && (result[7] == 0x00) && (result[6] == 0x00)))
                {
                    e = "转速不稳定";
                    return false;
                }
                if ((result[0] == 0x00) && (result[1] == 0x00) && (result[2] == 0x00))
                {
                    e = "串口失连,可能plasma断电";
                    return false;
                }
            }
            else
            {
                e = $"读取位置{ data[3] }返回值个数为{result.Count()}";
                return false;
            }
            #endregion
            #region 系统过流
            data[3] = 0x0E;
            result = Received(data);
            if (result.Count() == 8)
            {
                if (((result[3] != 0x00) && (result[7] == 0x00) && (result[6] == 0x00)))
                {
                    e = "系统过流";
                    return false;
                }
                if ((result[0] == 0x00) && (result[1] == 0x00) && (result[2] == 0x00))
                {
                    e = "串口失连,可能plasma断电";
                    return false;
                }
            }
            else
            {
                e = $"读取位置{ data[3] }返回值个数为{result.Count()}";
                return false;
            }
            #endregion
            #region 温度过高
            data[3] = 0x10;
            result = Received(data);
            if (result.Count() == 8)
            {
                if (((result[3] == 0x01) && (result[7] == 0x00) && (result[6] == 0x00)))
                {
                    e = "温度过高";
                    return false;
                }
                if ((result[0] == 0x00) && (result[1] == 0x00) && (result[2] == 0x00))
                {
                    e = "串口失连,可能plasma断电";
                    return false;
                }
            }
            else
            {
                e = $"读取位置{ data[3] }返回值个数为{result.Count()}";
                return false;
            }
            #endregion
            #region 功率不匹配
            data[3] = 0x0F;
            result = Received(data);
            if (result.Count() == 8)
            {
                if (((result[3] != 0x00) && (result[7] == 0x00) && (result[6] == 0x00)))
                {
                    e = "功率不匹配";
                    return false;
                }
                if ((result[0] == 0x00) && (result[1] == 0x00) && (result[2] == 0x00))
                {
                    e = "串口失连,可能plasma断电";
                    return false;
                }
            }
            else
            {
                e = $"读取位置{ data[3] }返回值个数为{result.Count()}";
                return false;
            }
            #endregion
            #region 试用时间到
            data[3] = 0x11;
            result = Received(data);
            if (result.Count() == 8)
            {
                if (((result[3] != 0x00) && (result[7] == 0x00) && (result[6] == 0x00)))
                {
                    e = "试用时间到";
                    // return false;
                }
                if ((result[0] == 0x00) && (result[1] == 0x00) && (result[2] == 0x00))
                {
                    e = "串口失连,可能plasma断电";
                    return false;
                }
            }
            else
            {
                e = $"读取位置{ data[3] }返回值个数为{result.Count()}";
                return false;
            }
            #endregion
            return true;
        }

        /// <summary>
        /// 获取Plasma当前功率 气压 01 04 00 00 00 01
        /// </summary>
        /// <param name="Power"></param>
        /// <param name="Air"></param>
        /// <returns></returns>
        public override bool GetPowerAndAir(ref int Power, ref int Air)
        {
            byte[] data = new byte[6];
            data[0] = 0x01;
            data[1] = 0x04;
            data[2] = 0x00;
            data[3] = 0x01;
            data[4] = 0x00;
            data[5] = 0x01;
            byte[] result = Received(data);
            if ((result[2] != 0x02))
            {
                return false;
            }
            Power = (((int)result[3]) << 8) + result[4];
            data[3] = 0x04;
            result = Received(data);
            if ((result[2] != 0x02))
            {
                return false;
            }
            Air = (((int)result[3]) << 8) + result[4];
            return true;
        }

        /// <summary>
        /// 把对应的指令经过处理后发送，并接收回应
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public override byte[] Received(byte[] data)
        {

            byte[] result;
            byte[] value = new byte[8];
            byte[] crc = new byte[2];
            data[3] = (byte)(data[3] - 1);
            crc = Crc16(data);
            value[0] = data[0];
            value[1] = data[1];
            value[2] = data[2];
            value[3] = data[3];
            value[4] = data[4];
            value[5] = data[5];
            value[6] = crc[0];
            value[7] = crc[1];
            if ((data[1] == 6) || (data[1] == 5))
                result = new byte[8];
            else
                result = new byte[6];


            result = SendAndReceiveData(value, 200);//COM_BASE提供的方法


            byte[] resultRetruen = new byte[8];
            if (data[1] != 6)
            {
                resultRetruen[0] = result[0];
                resultRetruen[1] = result[1];
                resultRetruen[2] = result[2];
                resultRetruen[3] = result[3];
                resultRetruen[4] = result[4];
                resultRetruen[5] = result[5];
                resultRetruen[6] = 0;
                resultRetruen[7] = 0;
            }
            else
                resultRetruen = result;
            return resultRetruen;

        }

    }
}

